package com.ly.cloud.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.ly.cloud.dto.ProfessionalInfoDto;
import com.ly.cloud.entity.ProfessionalInfoPo;
import com.ly.cloud.exception.CloudException;
import com.ly.cloud.mapper.ProfessionalInfoMapper;
import com.ly.cloud.service.ProfessionalInfoService;
import com.ly.cloud.utils.BeanCopyUtils;
import com.ly.cloud.vo.*;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @Description: 开设专业
 * @Author: pengshiqng
 * @Date: Create in 2018/12/13
 */
@Service
public class ProfessionalInfoServiceImpl implements ProfessionalInfoService {

    @Autowired
    private ProfessionalInfoMapper professionalInfoPoMapper;


    @Override
    public Page<ProfessionalInfoVo> selectPage(Page<ProfessionalInfoVo> page ,  ProfessionalInfoDto dto) {
        try {
            return page.setRecords(professionalInfoPoMapper.selectPage(page, dto));
        }catch (Exception e){
            throw new CloudException("查询异常，请重试!", e);
        }
    }

    @Override
    public int deleteById(String id) {
        try {
            return professionalInfoPoMapper.deleteById(id);
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("删除异常，请重试!", e);
        }
    }

    @Override
    public int deleteByIds(List<String> ids) {
        try {
            return professionalInfoPoMapper.deleteBatchIds(ids);
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("批量删除异常，请重试!", e);
        }
    }

    @Override
    public int insert(String loginUserName ,ProfessionalInfoDto dto) {
        try {
            //把dto对象转po对象
            ProfessionalInfoPo po = BeanCopyUtils.copyBeanProperties(dto, ProfessionalInfoPo.class);
            po.setCzr(loginUserName);
            po.setCzsj(new Date());
            return professionalInfoPoMapper.insert(po);
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("新增失败!", e);
        }
    }

    @Override
    public int uodateById(String loginUserName , ProfessionalInfoDto dto) {
        try {
            //把dto对象转po对象
            ProfessionalInfoPo po = BeanCopyUtils.copyBeanProperties(dto, ProfessionalInfoPo.class);
            po.setXgr(loginUserName);
            po.setXgsj(new Date());
            return professionalInfoPoMapper.updateById(po);
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("更新失败!", e);
        }
    }

    /**
     * 查询专业下拉列表
     * 根据院系代码过滤专业
     * @author caihuihai 2018-12-20
     */
	@Override
	public List<ProfessVo> findProListByDwh(String dwh) {
		try {
            return professionalInfoPoMapper.findProListByDwh(dwh);
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("查询失败!", e);
        }
	}
	
	/**
     * 查询全部专业,下拉列表
     * @author luhuacong 2018-12-28
     */
	@Override
	public List<ProfessionalInfoVo> selectAllPro() {
		
		try {
			List<ProfessionalInfoPo> poList = professionalInfoPoMapper.selectList(null);
			return BeanCopyUtils.copyListProperties(poList, ProfessionalInfoVo.class);
		} catch (Exception e) {
			throw new CloudException("查询失败!", e);
		}
        }

    @Override
    public List<DwSelectVO> tree(String id) {
        try {
            return professionalInfoPoMapper.tree(id);
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("查询失败!", e);
        }
    }

    /**
    * @Description: 查询培养层次 下拉通用
    * @author: pengshiqing
    * @date: 2018/12/29
    */
    @Override
    public List<SelectVo> pyccSelect() {
        try {
            return professionalInfoPoMapper.pyccSelect();
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("查询失败!", e);
        }
    }

    /**
    * @Description: 新增的时候 查询专业号是否存在
    * @author: pengshiqing
    * @date: 2019/1/4
    */
    @Override
    public Boolean checkedZyh(String zyh) {
        try {
            int result = professionalInfoPoMapper.checkedZyh(zyh);
            // 表示数据库没有此专业号
            if (result == 0){
                return false ;
            }
            return true ;
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("查询失败!", e);
        }
    }

    /**
    * @Description: 通过专业号查询专业信息
    * @author: pengshiqing
    * @date: 2019/1/4
    */
    @Override
    public ProfessionalInfoVo findProfessByZyh(String zyh) {
        try {
            ProfessionalInfoPo po = professionalInfoPoMapper.selectById(zyh);
            //把po对象转成对象
            return BeanCopyUtils.copyBeanProperties(po,ProfessionalInfoVo.class);
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("查询失败!", e);
        }
    }

    /**
    * @Description: 学科门类 下拉
    * @author: pengshiqing
    * @date: 2019/1/7
    */
    @Override
    public List<XkmlSelectVo> findXkmlSelect() {
        try {
           return professionalInfoPoMapper.findXkmlSelect();
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("查询失败!", e);
        }
    }

    /**
     * @Description: 大学科 下拉
     * @author: pengshiqing
     * @date: 2019/1/7
     */
    @Override
    public List<DxkSelectVo> findDxkSelect() {
        try {
            return professionalInfoPoMapper.findDxkSelect();
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("查询失败!", e);
        }
    }

    /**
     * @Description: 一级学科 下拉
     * @author: pengshiqing
     * @date: 2019/1/7
     */
    @Override
    public List<YjxkSelectVo> findYjxkSelect() {
        try {
            return professionalInfoPoMapper.findYjxkSelect();
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("查询失败!", e);
        }
    }

    /**
    * @Description: 专业类别 下拉
    * @author: pengshiqing
    * @date: 2019/1/8
    */
    @Override
    public List<ZylbSelectVo> findZylbSelect() {
        try {
            return professionalInfoPoMapper.findZylbSelect();
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("查询失败!", e);
        }
    }
}
